﻿using MCPlatform.Components;
using MCPlatform.UI.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.ViewModels.Shell
{
    public class ActivateViewModel : ViewModelBase
    {

        public string ExpirationDate 
        {
            get => _ExpirationDate; 
            set => Set(ref _ExpirationDate, value); 
        }
        private string _ExpirationDate = string.Empty;



        public string ActivationCode
        {
            get => _ActivationCode;
            set => Set(ref _ActivationCode, value);
        }
        private string _ActivationCode = string.Empty;


        public string ActivationStatus
        {
            get => _ActivationStatus;
            set => Set(ref _ActivationStatus, value);
        }
        private string _ActivationStatus = string.Empty;


        public event Action? ActivateSuccessEvent;

        ActivationManager activationManager = ActivationManager.GetInstance();

        public ActivateViewModel()
        {
            InitActivateTime();
        }

       


        private void InitActivateTime()
        {
            int remainingDays = activationManager.CheckActivationStatus();
            if (remainingDays == -1)
            {
                ExpirationDate = "未激活或已过期";

            }
            else if (remainingDays == -999)
            {
                ExpirationDate = "未激活";

            }
            else
            {
                ExpirationDate = "剩余天数：" + remainingDays;
            }
        }


        public void Activate()
        {
            string activationCode = ActivationCode;
            int Ret = activationManager.Activate(activationCode);
            if (Ret == -1)
            {
                ActivationStatus = "无效激活码";
                return;
            }
            else if (Ret == 999)
            {
                ActivationStatus = "已经永久，无需激活";
                return;
            }
            else
            {
                ActivationStatus = "激活成功";
            }
            InitActivateTime();

           

            ActivateSuccessEvent?.Invoke();


           // this.TryCloseAsync();
        }
    }
}
